package com.hq.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hq.pojo.Detailed;
import com.hq.pojo.Order;
import com.hq.service.OrderService;
import com.hq.util.FileUploadUtil;
import com.hq.util.SnowflakeIdUtils;
import com.sun.org.apache.xpath.internal.operations.Mod;
import jdk.nashorn.internal.ir.ReturnNode;
import org.aspectj.weaver.ast.Or;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.xml.soap.Detail;
import java.time.OffsetDateTime;
import java.util.List;

@Controller
public class OrderController {

    @Resource
    private OrderService orderService;
    @RequestMapping("toadd.do")
    public String toadd(Model model){
        if(!model.containsAttribute("snowid")){
            long l = new SnowflakeIdUtils(1, 3).nextId();
            model.addAttribute("snowid",l+"");
            int i=orderService.addOrder(l+"");
        }
        return "addOrder";
    }
    //909828118737719296
    //909827215863451600
    @RequestMapping("toaddDetail.do")
    public String toaddDetail(Model model){
        return "addDetail";
    }

    @RequestMapping("toOrder.do")
    public String toOrder(){
        return "addOrder";
    }
    @RequestMapping("addDetail.do")
    public String addDetail(Detailed detailed, Model model){
        System.out.println("======================================="+detailed);
        int i=orderService.addDetail(detailed);
        model.addAttribute("snowid",detailed.getOrder().getId()+"");
        return "forward:toOrder.do";
    }

    @RequestMapping("detailedList.do")
    @ResponseBody
    public  Object detailedList(String snowId){
        List<Detailed> list= orderService.detailedList(snowId);
        return list;
    }

    @RequestMapping("add.do")
    @ResponseBody
    public Object add(Order order, MultipartFile file, HttpSession session){
        if(file.getSize()>0){
            String upload = FileUploadUtil.load(session, file, "upload");
            order.setPic(upload);
        }
        int i=orderService.add(order);
        return i;
    }

    @RequestMapping("list.do")
    public String  list(@RequestParam(value="cpage",defaultValue ="1") Integer cpage,Model model){
        PageHelper.startPage(cpage,3);
        List list = orderService.list(null);
        PageInfo pageInfo = new PageInfo(list);
        model.addAttribute("pageInfo",pageInfo);
        return "list";
    }
}
